from tkinter import *
from tkinter.ttk import *
import tkinter as tk
import common


class logPage():
    def __init__(self,root):
        self.__win(root)
        self.tk_table_page_data_list = self.__tk_table_page_data_list()
        self.tk_button_s371myqb = self.__tk_button_s371myqb()
        self.tk_button_s371myde = self.__tk_button_s371myde()
        self.tk_label_xc72126q = self.__tk_label_xc72126q()
        self.tk_input_cxcgzy5s = self.__tk_input_cxcgzy5s()
        self.tk_label_xc72126n = self.__tk_label_xc72126n()
        self.__event_bind()
        self.pnum = 0
        self.rownum = 10
        self.queryListData(0,self.rownum)
        self.tk_button_s371myqb.config(state=tk.DISABLED)
        self.tk_input_cxcgzy5s.insert(0,str(self.rownum))


    def __win(self,root):
        self.newPage = tk.Toplevel(root)
        self.newPage.title("日志查看")
        width = 740
        height = 310

        screenwidth = self.newPage.winfo_screenwidth()
        screenheight = self.newPage.winfo_screenheight()
        geometry = '%dx%d+%d+%d' % (width, height, (screenwidth - width) / 2, (screenheight - height) / 2)
        self.newPage.geometry(geometry)
        self.newPage.resizable(width=False, height=False)

    def scrollbar_autohide(self, vbar, hbar, widget):
        """自动隐藏滚动条"""
        def show():
            if vbar: vbar.lift(widget)
            if hbar: hbar.lift(widget)
        def hide():
            if vbar: vbar.lower(widget)
            if hbar: hbar.lower(widget)

        hide()
        widget.bind("<Enter>", lambda e: show())
        if vbar: vbar.bind("<Enter>", lambda e: show())
        if vbar: vbar.bind("<Leave>", lambda e: hide())
        if hbar: hbar.bind("<Enter>", lambda e: show())
        if hbar: hbar.bind("<Leave>", lambda e: hide())
        widget.bind("<Leave>", lambda e: hide())

    def v_scrollbar(self, vbar, widget, x, y, w, h, pw, ph):
        widget.configure(yscrollcommand=vbar.set)
        vbar.config(command=widget.yview)
        vbar.place(relx=(w + x) / pw, rely=y / ph, relheight=h / ph, anchor='ne')

    def h_scrollbar(self, hbar, widget, x, y, w, h, pw, ph):
        widget.configure(xscrollcommand=hbar.set)
        hbar.config(command=widget.xview)
        hbar.place(relx=x / pw, rely=(y + h) / ph, relwidth=w / pw, anchor='sw')

    def create_bar(self, master, widget, is_vbar, is_hbar, x, y, w, h, pw, ph):
        vbar, hbar = None, None
        if is_vbar:
            vbar = Scrollbar(master)
            self.v_scrollbar(vbar, widget, x, y, w, h, pw, ph)
        if is_hbar:
            hbar = Scrollbar(master, orient="horizontal")
            self.h_scrollbar(hbar, widget, x, y, w, h, pw, ph)
        self.scrollbar_autohide(vbar, hbar, widget)

    def __tk_table_page_data_list(self):
        # 表头字段 表头宽度
        columns = {"ID":75,"类型":100,"时间":160,"内容":390}
        tk_table = Treeview(self.newPage, show="headings", columns=list(columns))
        """
            定义滚动条控件
            orient为滚动条的方向，vertical--纵向，horizontal--横向
            command=self.tree.yview 将滚动条绑定到treeview控件的Y轴
        """
        VScroll1 = Scrollbar(tk_table, orient='vertical', command=tk_table.yview)
        VScroll1.place(relx=0.980, rely=0.008, relwidth=0.024, relheight=0.958)
        # 给treeview添加配置
        tk_table.configure(yscrollcommand=VScroll1.set)

        for text, width in columns.items():  # 批量设置列属性
            tk_table.heading(text, text=text, anchor='center')
            tk_table.column(text, anchor='center', width=width, stretch=False)  # stretch 不自动拉伸
        tk_table.place(x=0, y=5, width=740, height=256)
        return tk_table
    def __tk_label_xc72126q(self):
        label = Label(self.newPage,text="每页显示",anchor="center", )
        label.place(x=40, y=270, width=200, height=30)
        return label
    def __tk_input_cxcgzy5s(self):
        ipt = Entry(self.newPage, )
        ipt.place(x=200, y=270, width=80, height=30)
        return ipt
    def __tk_label_xc72126n(self):
        label = Label(self.newPage,text="条",anchor="center", )
        label.place(x=300, y=270, width=50, height=30)
        return label
    def __tk_button_s371myqb(self):
        btn = Button(self.newPage, text="上一页", takefocus=False, )
        btn.place(x=352, y=270, width=124, height=30)
        return btn
    def __tk_button_s371myde(self):
        btn = Button(self.newPage, text="下一页", takefocus=False, )
        btn.place(x=500, y=270, width=124, height=30)
        return btn

    def __event_bind(self):
        self.tk_button_s371myqb.bind("<Button-1>", self.previousPage)
        self.tk_button_s371myde.bind("<Button-1>", self.nextPage)

    def queryListData(self,p,row):
        for item in self.tk_table_page_data_list.get_children():
            self.tk_table_page_data_list.delete(item)
        _sql_data = common.query_data(f"select * from push_logs ORDER BY id DESC LIMIT {p},{row};",2)
        for itm in _sql_data:
            __type = "计算" if itm[4] == 1 else "上报"
            display = (itm[0],__type,itm[2],itm[3])
            self.tk_table_page_data_list.insert("", tk.END, values=display)

    #上一页
    def previousPage(self,event):
        self.getPageRows()
        self.pnum = self.pnum - 1
        _pnum = self.pnum * self.rownum
        if _pnum >= 0:
            self.tk_button_s371myde.config(state=tk.NORMAL)
            self.queryListData(_pnum,self.rownum)
            if _pnum == 0:
                self.tk_button_s371myqb.config(state=tk.DISABLED)
        else:
            self.pnum = self.pnum + 1
            self.tk_button_s371myqb.config(state=tk.DISABLED)

    #下一页
    def nextPage(self,event):
        self.getPageRows()
        #统计日志有多少条
        _sql_num = common.query_data(f"select COUNT(id) from push_logs;",1)[0]
        self.pnum = self.pnum + 1
        _pnum = self.pnum * self.rownum
        if _pnum <= _sql_num:
            self.tk_button_s371myqb.config(state=tk.NORMAL)
            self.queryListData(_pnum,self.rownum)
            if (_pnum + self.rownum) >= _sql_num:
                self.tk_button_s371myde.config(state=tk.DISABLED)
        else:
            self.pnum = self.pnum - 1
            self.tk_button_s371myde.config(state=tk.DISABLED)

    def getPageRows(self):
        ypnum = int(self.tk_input_cxcgzy5s.get())
        if ypnum:
            self.rownum = ypnum
        else:
            self.rownum = 10